package com.fingard.dsp.bank.directbank.calyon01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ITransaction;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7515TodayTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.Req7516HisTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.RetAtsTrans;
import com.fingard.dsp.bank.reqrespobj.transaction.detail.RetAtsTransDetail;
import com.fingard.io.FileHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

public class CALYON01Transaction extends CALYON01Base implements ITransaction {
    @Override
    public RetAtsTrans d_7516_HisTrans(Req7516HisTrans p_req7516) throws Exception {
        //查询时间校验
        String[] dateAry=getQueryDate(p_req7516.dateFrom,p_req7516.dateTo);
        p_req7516.dateFrom=dateAry[0];
        p_req7516.dateTo=dateAry[1];
        //查询明细数据
        RetAtsTrans retAts = new RetAtsTrans(p_req7516.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo ="";
        retAts.writeStart();

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader reader = null;
        try{
            String accountNum = p_req7516.actNum;
            Date dateStart = Format.Date8Format.parse(p_req7516.dateFrom);
            Date dateEnd = Format.Date8Format.parse(p_req7516.dateTo);
            Calendar calendar = Calendar.getInstance();
            while (!dateStart.after(dateEnd)){
                String dealedPath = getBankConfig().getValueAsString("DealedPath")+File.separator+
                        Format.Date8Format.format(dateStart)+File.separator+"MT940"+File.separator+
                        accountNum+"_7516.txt";
                if(!FileHelper.existFile(dealedPath)){
                    calendar.setTime(dateStart);
                    calendar.add(Calendar.DAY_OF_MONTH, 1);
                    dateStart = calendar.getTime();
                    continue;
                }
                String readLine = "";
                fis = new FileInputStream(dealedPath);
                isr = new InputStreamReader(fis, getCharset());
                reader = new BufferedReader(isr);
                while ((readLine=reader.readLine())!=null) {
                    //账号 | 币种 | 交易日期| 交易时间| 计息日期 | 交易金额 |借贷标识| 账户余额 |对方账号|
//                    对方户名 | 对方银行 |用途| 流水号|
                    String[] transDetail = readLine.split("\\|");
                    if(transDetail.length>12){
                        RetAtsTransDetail retDetail = new RetAtsTransDetail();
                        retDetail.actArea=p_req7516.actArea;
                        retDetail.actNum = transDetail[0].trim();
                        retDetail.cur =convertCurToATS(transDetail[1].trim());
                        retDetail.tranDate = transDetail[2].trim();
                        retDetail.tranTime = transDetail[3].trim();
                        retDetail.valueDate = transDetail[4].trim();
                        retDetail.tranAmt = transDetail[5].trim();
                        retDetail.recAccNum = transDetail[8].trim();
                        retDetail.cdFlag = "C".equals(transDetail[6].trim())?"2":"1";
                        retDetail.accBal = transDetail[7].trim();
                        retDetail.recAccName = transDetail[9].trim();
                        retDetail.recAccBank = transDetail[10].trim();
                        String[] transArray=transDetail[11].split("##");
                        if(transArray.length>1){
                            if(transArray[0].trim().length()>256){
                                retDetail.usage = transArray[0].trim().substring(0,256);
                                retDetail.comment = transArray[0].trim().substring(256)+" "+transArray[1];
                            }else {
                                retDetail.usage = transArray[0].trim();
                                retDetail.comment = transArray[1];
                            }
                        }else {
                            if(transArray[0].trim().length()>256){
                                retDetail.usage = transArray[0].trim().substring(0,256);
                                retDetail.comment = transArray[0].trim().substring(256);
                            }else {
                                retDetail.usage = transArray[0].trim();
                            }
                        }
                        retDetail.askNumber=transDetail[12].trim();
                        retAts.writeRetDetail(retDetail);
                    }
                }
                reader.close();
                calendar.setTime(dateStart);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                dateStart = calendar.getTime();
            }
        }catch (Exception e){
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询历史明细异常："+e);
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7516.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("明细为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsTrans d_7515_TodayTrans(Req7515TodayTrans p_req7515) throws Exception {
        RetAtsTrans retAts = new RetAtsTrans(p_req7515.reqHeader);
        //写返回ATS的统一头部信息
        String tmpRespCode = "";
        String tmpRespInfo ="";
        retAts.writeStart();

        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader reader = null;
        try{
            String accNum = p_req7515.actNum;
            String date =Format.Date8Format.format(new Date());
            String dealedPath = getBankConfig().getValueAsString("DealedPath")+File.separator+date+
                    File.separator+"MT942"+File.separator+accNum+"_7515.txt";
            if(FileHelper.existFile(dealedPath)){
                fis = new FileInputStream(dealedPath);
                isr = new InputStreamReader(fis,"UTF-8");
                reader = new BufferedReader(isr);
                String readLine ="";
                while ((readLine=reader.readLine())!=null){
                    // 账号 | 币种 | 交易日期| 交易时间| 计息日期 | 交易金额 |借贷标识| 账户余额 |对方账号|
                    // 对方户名 | 对方银行 |用途 | 流水号|
                    String[] transDetail = readLine.split("\\|");
                    if(transDetail.length>12) {
                        RetAtsTransDetail detail = new RetAtsTransDetail();
                        detail.actArea = p_req7515.actArea;
                        detail.actNum = transDetail[0].trim();
                        detail.cur = convertCurToATS(transDetail[1].trim());
                        detail.tranDate = transDetail[2].trim();
                        detail.tranTime = transDetail[3].trim();
                        detail.valueDate = transDetail[4].trim();
                        detail.tranAmt = transDetail[5].trim();
                        detail.cdFlag = "C".equals(transDetail[6].trim()) ? "2" : "1";
                        detail.recAccNum = transDetail[8].trim();
                        detail.accBal = transDetail[7].trim();
                        detail.recAccBank = transDetail[10].trim();
                        detail.recAccName = transDetail[9].trim();
                        String[] transArray=transDetail[11].split("##");
                        //getRecInformationMap
                        if(transArray.length>1){
                            if(transArray[0].trim().length()>256){
                                detail.usage = transArray[0].trim().substring(0,256);
                                detail.comment = transArray[0].trim().substring(256)+" "+transArray[1];
                            }else {
                                detail.usage = transArray[0].trim();
                                detail.comment = transArray[1];
                            }
                        }else {
                            if(transArray[0].trim().length()>256){
                                detail.usage = transArray[0].trim().substring(0,256);
                                detail.comment = transArray[0].trim().substring(256);
                            }else {
                                detail.usage = transArray[0].trim();
                            }
                        }
                        detail.askNumber=transDetail[12].trim();
                        retAts.writeRetDetail(detail);
                    }
                }
                reader.close();
            }
        }catch (Exception e){
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
            WriteBankLogLn("查询今日明细异常："+e);
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAtsTrans(p_req7515.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("明细为空");
        }
        retAts.writeEnd();
        return retAts;
    }

    public Map<String, String> getRecInformationMap(String string86) throws Exception {
        //定义元素
        String recAct = "";//对方账户
        String recName = "";//对方名称
        String recBankName="";//对方银行名称
        String recDetail = "";//备注
        String[] detailsplit = string86.split("/");
        List<String> list = Arrays.asList(detailsplit);
        //对方账户
        if (list.indexOf("AC") != -1) {
            recAct = list.get(list.indexOf("AC") + 1).trim();
            recAct = recAct.replaceAll("\r\n", "");
        } else {
            //借贷标识为C时，银行未返回对方账户
            recAct = "";
        }
        //对方名称
        if (list.indexOf("BENM") != -1) {
            recName = list.get(list.indexOf("BENM") + 1).trim();
            recName = recName.replaceAll("\r\n", "");
        }
        if (list.indexOf("ORDP") != -1) {
            recName = list.get(list.indexOf("ORDP") + 1).trim();
            recName = recName.replaceAll("\r\n", "");
        }
        //用途
        if (list.indexOf("REMI") != -1) {
            recDetail = list.get(list.indexOf("REMI") + 1).trim();
            recDetail = recDetail.replaceAll("\r\n", "");
        }
        //返回值
        Map<String,String> map=new HashMap<String, String>();
        map.put("recAct",recAct);
        map.put("recName",recName);
        map.put("recBankName",recBankName);
        map.put("recDetail",recDetail);
        return map;
    }


}
